<?php

namespace App\Http\Controllers\Wenzhang;

//use Illuminate\Http\Request;
use Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\Book;
use App\Models\Wenzhang;
use App\Models\Wenzhang_dianzan;
use App\Models\Wenzhang_pinglun;
use App\Models\UsersReg;

use Session;
use Redirect;

class WenzhangController extends Controller
{
    
    public function pubwenzhang()
    {
        //发布帖子
        return view('wenzhang/pubwenzhang');
    }

    public function wenzhang_img_upload(Request $request)
    {
        //上传图片处理
        //return $_FILES;

        // //获取临时存储路径
        // $file = $_FILES["images"]["tmp_name"];
        
        // //文件名前缀
        // $newfile_prv = 'wenzhang_' .time() . str_random(10);
        // //文件扩展名字
        // $pathinfo = pathinfo($fileName);
        // $extension = $pathinfo['extension'];
        // $newFile_Path = $dir . '/' . $newfile_prv . '.' . $extension; 
        // $tmpFile_Path = $fileName; 
        // //保存文件使用                    
        // move_uploaded_file($tmpFile_Path, $newFile_Path);

        // //这里拼接返回的格式
        // $ret = [];
        // $data[] =  'http://localhost/laravel-books-v5.1.11/public/wenzhang_img/image7.jpg';
        // $ret['errno'] = 0;
        // $ret['data'] = $data;

        // return $ret;
        // //return json_encode($ret,JSON_UNESCAPED_SLASHES);

        if ($file = $request::hasFile('file')) //有选择图片
        {  
            //获得上传的图片
            $file = $request::file('file');          

            //获取UploadFile实例
            if ( $file->isValid()) //判断文件是否有效
            {             
                //$filename = $file->getClientOriginalName(); //文件原名称
                $extension = $file->getClientOriginalExtension(); //扩展名
                $filename = 'wenzhang_' .time() . str_random(10) . "." . $extension; //重命名
                $file->move('wenzhang_img/', $filename); //移动至指定目录
                $path = url("wenzhang_img/$filename"); 
                $data = [ 'status' => 0, 'path' => $path]; 
                return $data;
            } 
        } 
        return '未获取到上传文件或上传过程出错';
    }

    public function wenzhang_save()
    {
        //保存文章内容
        $title = request::input('title');
        $content = request::input('content');

        //preg_match_all("/(href|src)=([\"|']?)([^\"'>]+.(jpg|JPG|jpeg|JPEG|gif|GIF|png|PNG))/i", $content, $img);

        //找出内容里的图片
        preg_match_all('#src="([^"]+?)"#', $content, $img);
        //dd($img[0][0]);

        //获得数组的长度
        $img_count = count($img[1]);
        $imglist = '';
        if($img_count > 0)
        {
            foreach($img[1] as $value)
            {
                $imgtemp = substr($value, -33) ; //保留需要的字符串，图片地址
                //$imglist = gettype($imgtemp); //判断类型
                $imglist = $imglist . ',' . $imgtemp; //拼接图片的字符串
            }
            $imglist = substr($imglist, 1); //去掉拼接字符创的第一个逗号
        }         

        //$imglist = implode(',',$img[1]);

        $wenzhang = new Wenzhang;
        $wenzhang->uid = Session::get('nameid');
        $wenzhang->title = $title;
        $wenzhang->content = $content;
        $wenzhang->imglist = $imglist;
        $wenzhang->add_time = time();
        $wenzhang->save();
        return 1;

    }

    public function lists()
    {
        //文章列表页面
        $wenzhanglists = wenzhang::orderBy('add_time', 'desc')->paginate(10);
        foreach($wenzhanglists as $wenzhanglist)
        {
            $imglist = $wenzhanglist->imglist;
            if($imglist != null)
            { //如果有图片                
                $imglist_arr = explode(',' , $imglist);
                //取前三个数组，最多显示三个图片
                $imglist_arr3 = array_slice($imglist_arr,0,3); 
                //图片数组
                $wenzhanglist['imglist'] = $imglist_arr3; 
                //图片的总数量
                $wenzhanglist['imgcount'] = count($imglist_arr);                
            }
            else
            {
                //没有图片
                $wenzhanglist['imgcount'] = 0;
            }
            //对应的会员资料
            $wenzhanglist['users'] = $wenzhanglist->hasuserinfo;   
            //对应的书籍资料
            $wenzhanglist['books'] = Book::select('book_name')->where('book_id', $wenzhanglist->bookid)->first();
            //显示所有图片
            $wenzhanglist['PhotoSwipe'] = $imglist;
            //评论数量
            $wenzhanglist['pingluncount'] = wenzhang_pinglun::where('wenzhang_id', $wenzhanglist->id)->count();
            //点赞数量
            $wenzhanglist['dianzan'] = wenzhang_dianzan::where('wenzhang_id', $wenzhanglist->id)->count();
        }
        //dd($wenzhanglists);

        return view('wenzhang/lists',array(
            'wenzhanglists' => $wenzhanglists
        ));
    }

    public function dianzan()
    {
        //点赞
        $nameid = Session::get('nameid');
        $wenzhang_id = request::input('wenzhangid');
        if(!$nameid)
        {
            //没有登录
            return 1;
        }

        $wenzhang_dianzan = wenzhang_dianzan::where('uid', $nameid)->where('wenzhang_id', $wenzhang_id)->first(); 
        if($wenzhang_dianzan == null)
        {        
            //记录点赞用户
            $dianzan = new wenzhang_dianzan;
            $dianzan->wenzhang_id = $wenzhang_id;
            $dianzan->uid = $nameid;
            $dianzan->save();
            return 2;
        }
        else
        {
            //已经点过赞了
            return 3;
        }
    }

    public function wenzhangpinglun($id)
    {
        //帖子的评论
        $wenzhanglist = wenzhang::find($id);
        //阅读次数增加
        $wenzhanglist->click = $wenzhanglist->click + 1;
        $wenzhanglist->save();

        $imglist = $wenzhanglist->imglist;
        if($imglist != null)
        { 
            //图片数组               
            $imglist_arr = explode(',' , $imglist);             
        }
        else
        {
            //没有图片
            $imglist_arr = 0;
        }            

        //对应的会员资料
        $users = UsersReg::find($wenzhanglist->uid);

        //对应的评论
        $wenzhang_pingluns = wenzhang_pinglun::where('wenzhang_id', $id)->orderBy('add_time', 'desc')->get();   
        foreach($wenzhang_pingluns as $wenzhang_pinglun)
        {
            //评论对应的会员资料
            $wenzhang_pinglun['users'] = $wenzhang_pinglun->hasuserinfo;
        }

        //对应的点赞数量
        $dianzan_count = wenzhang_dianzan::where('wenzhang_id', $id)->count();

        return view('wenzhang/wenzhangpinglun',array(
            'wenzhanglist' => $wenzhanglist,
            'imglist_arr' => $imglist_arr,
            'imglist' => $imglist,
            'users'  => $users,
            'id'   =>  $id,
            'wenzhang_pingluns' => $wenzhang_pingluns,
            'dianzan_count' => $dianzan_count
        ));
    }

    public function pubwenzhang_pinglun($id)
    {
        //发布文章评论页面
        return view('wenzhang/pubwenzhang_pinglun',array(
            'id' => $id
        ));
    }

    public function pubwenzhang_pinglun_post()
    {
        //保存发布的书摘评论
        $wenzhangid = request::input('wenzhangid');
        $content = request::input('content');

        $wenzhang_pinglun = new wenzhang_pinglun;
        $wenzhang_pinglun->uid = Session::get('nameid');
        $wenzhang_pinglun->wenzhang_id = $wenzhangid;
        $wenzhang_pinglun->content = $content;
        $wenzhang_pinglun->add_time = time();
        $wenzhang_pinglun->save();

        return 'ok';
    }

}
